package com.open.capacity.salary.controller;

import java.util.List;
import java.util.Map;

import com.open.capacity.commons.Result;
import com.open.capacity.vo.TreeVo;
import io.swagger.annotations.Api;
import org.apache.commons.collections.list.TreeList;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import com.open.capacity.salary.entity.GzItem;
import com.open.capacity.salary.service.GzItemService;

import com.open.capacity.commons.PageResult;

import javax.validation.Valid;

/**
 * 工资系统款项表
 *
 * @author
 * @email
 * @date 2021-08-02 14:26:51
 */
@RestController
@RequestMapping("gzitems")
@Api(tags = "工资系统款项表")
public class GzItemController {

    @Autowired
    private GzItemService gzItemService;

    /**
     * 初始化默认数据
     */
    @RequestMapping("/init")
    @PreAuthorize("hasAnyAuthority('gz_item:gzitem:init')")
    public Result init(String companyId, String ztId) {
        gzItemService.init(companyId, ztId);
        return Result.succeed("初始化成功");
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @PreAuthorize("hasAnyAuthority('gz_item:gzitem:list')")
    public PageResult list(@RequestParam Map<String, Object> params) {
        PageResult pageResult = gzItemService.pageAll(params);
        return pageResult;
    }


    /**
     * 查询款项tree
     *
     * @param companyId 公司id
     * @param parentId  上级id
     * @return
     */
    @GetMapping("/tree")
    public Result<List<TreeVo>> treeItems(@RequestParam(value = "companyId") String companyId,
                                          @RequestParam(value = "ztId") String ztId,
                                          @RequestParam(value = "parentId", defaultValue = "0") String parentId) {
        List<GzItem> items = gzItemService.findByZtId(companyId, ztId);
        List<TreeVo> treeList = new TreeList();
        items.forEach(m -> {
            TreeVo node = new TreeVo();
            node.setId(m.getId());
            node.setText(m.getName());
            node.setValue(m.getId());
            node.setShowcheck(false);
            node.setCheckstate(0);
            node.setIsexpand(false);
            node.setComplete(false);
            node.setParentId(m.getParentId());

            treeList.add(node);
        });

        return Result.succeed(TreeVo.RecursiveBuilder(treeList, parentId));
    }

    /**
     * 款项列表
     */
    @RequestMapping("/dropdownlist")
    public Result<List<TreeVo>> dropdownlist(String companyId, String ztId, @RequestParam(value = "showCheck", required = false, defaultValue = "1") Integer showCheck) {
        List<GzItem> list = gzItemService.findByZtId(companyId, ztId);

        List<TreeVo> treeList = new TreeList();
        for (GzItem value : list) {
            TreeVo node1 = new TreeVo();
            node1.setId(value.getId());
            node1.setText(value.getName());
            node1.setTitle(value.getName());
            node1.setValue(value.getId());
            node1.setShowcheck(showCheck != null && showCheck == 0 ? false : true);
            node1.setIsexpand(false);
            node1.setComplete(true);
            node1.setParentId("0");
            treeList.add(node1);
        }

        return Result.succeed(treeList);
    }

    /**
     * 款项列表
     */
    @RequestMapping("/dropdownlist2")
    public Result<List<TreeVo>> dropdownlist2(String companyId, String ztId, @RequestParam(value = "showCheck", required = false, defaultValue = "1") Integer showCheck) {
        List<String> list = gzItemService.findByZtIds(companyId, ztId);

        List<TreeVo> treeList = new TreeList();
        for (String value : list) {
            TreeVo node1 = new TreeVo();
            node1.setId(value);
            node1.setText(value);
            node1.setTitle(value);
            node1.setValue(value);
            node1.setShowcheck(showCheck != null && showCheck == 0 ? false : true);
            node1.setIsexpand(false);
            node1.setComplete(true);
            node1.setParentId("0");
            treeList.add(node1);
        }

        return Result.succeed(treeList);
    }

    /**
     * 查询款项名称
     */
    @RequestMapping("/getName")
    public Result getNames(String keyValue) {
        GzItem model = gzItemService.findById(keyValue);
        return Result.succeed(model.getName(), "success");
    }


    /**
     * 查询单条记录
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAnyAuthority('gz_item:gzitem:list')")
    public Result findById(String id) {
        return Result.succeed(gzItemService.findById(id));
    }

    /**
     * 保存
     */
    @RequestMapping("/saveOrUpdate")
    @PreAuthorize("hasAnyAuthority('gz_item:gzitem:save')")
    public Result save(@RequestBody @Valid GzItem gzItem, String keyValue) {
        if (StringUtils.isEmpty(keyValue)) {
            gzItemService.save(gzItem);
        } else {
            gzItem.setId(keyValue);
            gzItemService.update(gzItem);
        }
        return Result.succeed("保存成功");
    }

    /**
     * 设置公式
     */
    @RequestMapping("/setFormula")
    public Result setFormula(@RequestBody @Valid GzItem gzItem, String keyValue) {
        gzItemService.setFormula(keyValue, gzItem.getFormula());
        return Result.succeed("设置成功");
    }


    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAnyAuthority('gz_item:gzitem:delete')")
    public Result delete(@PathVariable String id) {
        gzItemService.delete(id);
        return Result.succeed("删除成功");
    }

}
